package com.huaxin.hxcloud.work;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.huaxin.common.BaseActivity;
import com.huaxin.common.Constants;
import com.huaxin.common.Entity.DataInfo;
import com.huaxin.common.Entity.TargetRunStataDTO;
import com.huaxin.common.tools.HxTransmitRequestHelper;
import com.huaxin.common.tools.Log;
import com.huaxin.common.view.BottomSheetDialog;
import com.huaxin.hxcloud.R;
import com.huaxin.hxcloud.activity.ChartActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author lwjing
 * @date 2018/1/23
 * @Desc 碧水源指标界面
 * Copyright HUAXIN DATA. All rights reserved.
 */
public class HxTargetRunStataActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar      mToolbar;
    @BindView(R.id.rv_target_stata)
    RecyclerView rv_station_target;

    private String mStationCode;
    private int    mStationType;
    private String mStationName;
    private String mDataItemId;
    List<DataInfo> dataInfos = new ArrayList<>();
    private TargetRunStataAdapter mAdapter;
    private TargetRunStataAdapter mMAdapter;
    private View                  emptyView;

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        if (!extras.containsKey(Constants.IK_STATION_CODE)) {
            finish();
        }
        mStationCode = extras.getString(Constants.IK_STATION_CODE);
        mStationType = extras.getInt(Constants.IK_STATION_TYPE, 1);
        mStationName = extras.getString(Constants.IK_STATION_NAME);
        mDataItemId = extras.getString(Constants.IK_DATAITEM_ID);
        Log.i("lwjing", "stationcode= " + mStationCode + "  mStationName=" + mStationName);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_target_run_stata;
    }

    @Override
    protected int getLoadFrameId() {
        return R.id.content;
    }

    @Override
    public void initView() {
        //  super.initView();
        mToolbar.setTitle(mStationName);
        setSupportActionBar(mToolbar);

        LinearLayoutManager manager = new LinearLayoutManager(this);
        mAdapter = new TargetRunStataAdapter(R.layout.run_stata_item, dataInfos);
        rv_station_target.setAdapter(mAdapter);
        rv_station_target.setLayoutManager(manager);

        //空布局
        emptyView = LayoutInflater.from(this).inflate(R.layout.item_empty_nodata_center, (ViewGroup) rv_station_target.getParent(), false);

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                final DataInfo dataInfo = dataInfos.get(i);
                //弹出底部弹框
                final String[] array = {"查看历史数据"};

                final BottomSheetDialog dialog = new BottomSheetDialog(HxTargetRunStataActivity.this, array, new
                        BottomSheetDialog.OnItemClickListener() {

                            @Override
                            public void onItemClick(int position) {
                                if (position == 0) {
                                    // 跳转到历史数据
                                    Intent intent = new Intent(HxTargetRunStataActivity.this, ChartActivity.class);
                                    intent.putExtra("fullCode", dataInfo.getFullCode());
                                    startActivity(intent);
                                }
                            }
                        });
                dialog.show();
            }
        });
    }

    @Override
    protected void requestData() {
        //super.requestData();
        if (mStationType ==2){
            apiFindDataInfoByStationCode();
        }else{
            apiFindDataItemByDataTypeAndSc();
        }

    }

    /**
     * 通过站点获取指标
     */
    private void apiFindDataInfoByStationCode() {
        updateLoadStatus(LOADING);
        Log.e("lwjing", "stationcode= " + mStationCode);
        Call<TargetRunStataDTO> call = HxTransmitRequestHelper.getRequestApi().getFindDataInfoByStationCode(mStationCode);
        autoClose(call);
        call.enqueue(new Callback<TargetRunStataDTO>() {

            @Override
            public void onResponse(Call<TargetRunStataDTO> call, Response<TargetRunStataDTO> response) {
                TargetRunStataDTO runStataDTO = response.body();
                if (response.isSuccessful() && runStataDTO != null) {
                    updateLoadStatus(LOAD_SUCCESS);
                    List<DataInfo> dataInfoList = runStataDTO.getDataInfoList();
                    if (dataInfoList != null && dataInfoList.size() > 0) {
                        Log.i("lwjing", "stationtargetDto  size: " + dataInfoList.size());
                        //数据初始化
                        dataInfos.clear();
                        dataInfos.addAll(dataInfoList);
                        mAdapter.setNewData(dataInfos);

                    } else {
                        //设置null布局
                        mAdapter.setEmptyView(emptyView);
                    }

                } else {
                    updateLoadStatus(LOAD_ERROR);
                }
            }

            @Override
            public void onFailure(Call<TargetRunStataDTO> call, Throwable t) {
                updateLoadStatus(LOAD_FAILURE);
            }
        });
    }

    /**
     * mDataItemId 获取相关指标
     */
    private void apiFindDataItemByDataTypeAndSc() {
        updateLoadStatus(LOADING);
        Log.e("lwjing", "stationcode= " + mStationCode + "  mStationType=" + mStationType + " mDataItemId=" + mDataItemId);
        Call<TargetRunStataDTO> call = HxTransmitRequestHelper.getRequestApi().getFindDataItemByDataTypeAndSc(mStationCode, mStationType + "", mDataItemId);
        autoClose(call);
        call.enqueue(new Callback<TargetRunStataDTO>() {

            @Override
            public void onResponse(Call<TargetRunStataDTO> call, Response<TargetRunStataDTO> response) {
                TargetRunStataDTO runStataDTO = response.body();
                if (response.isSuccessful() && runStataDTO != null) {
                    updateLoadStatus(LOAD_SUCCESS);
                    List<DataInfo> dataInfoList = runStataDTO.getDataInfoList();
                    if (dataInfoList != null && dataInfoList.size() > 0) {
                        Log.i("lwjing", "stationtargetDto  size: " + dataInfoList.size());
                        //数据初始化
                        dataInfos.clear();
                        dataInfos.addAll(dataInfoList);
                        mAdapter.setNewData(dataInfos);

                    } else {
                        //设置null布局
                        mAdapter.setEmptyView(emptyView);
                    }

                } else {
                    updateLoadStatus(LOAD_ERROR);
                }
            }

            @Override
            public void onFailure(Call<TargetRunStataDTO> call, Throwable t) {
                updateLoadStatus(LOAD_FAILURE);
            }
        });
    }

    class TargetRunStataAdapter extends BaseQuickAdapter<DataInfo, BaseViewHolder> {
        //开关量 0关 1开 2故障 other半开 模拟量 double
        public final String open      = 1 + "";
        public final String close     = 0 + "";
        public final String exception = 2 + "";


        public TargetRunStataAdapter(int layoutResId, @Nullable List<DataInfo> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder baseViewHolder, DataInfo dataInfo) {
            baseViewHolder.setText(R.id.tv_name, dataInfo.getName());
            TextView tv_stata=baseViewHolder.getView(R.id.tv_stata);
            if ("0".equals(dataInfo.getDataMold())){
                if (dataInfo.getValue().equals(open)) {
                    tv_stata.setText("开");
                    tv_stata.setTextColor(getResources().getColor(R.color.state_green));
                } else if (dataInfo.getValue().equals(close)) {
                    tv_stata.setText("关");
                    tv_stata.setTextColor(getResources().getColor(R.color.red));
                } else if (dataInfo.getValue().equals(exception)) {
                    tv_stata.setText("自动");
                    tv_stata.setTextColor(getResources().getColor(R.color.result_view));
                } else {
                    tv_stata.setText("--");
                    tv_stata.setTextColor(getResources().getColor(R.color.result_view));
                }
            }else{
                tv_stata.setText(dataInfo.getValue() + dataInfo.getUnit());
                tv_stata.setTextColor(getResources().getColor(R.color.result_view));
            }

        }

    }
}
